package liverdungeon.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.AffineTransform;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.swing.*;
import liverdungeon.Account;
import liverdungeon.Global;
import liverdungeon.Level;
import liverdungeon.Position;
import liverdungeon.actors.Actor;
import liverdungeon.actors.Hero;
import liverdungeon.gamemode.DungeonClient;
import liverdungeon.gamemode.DungeonServer;

public class MainMenuApplet extends JApplet
{
    //  MAIN DIMENSIONS
    public static int WIDTH, HEIGHT;
    public static int A_WIDTH, A_HEIGHT;
    public static int A_X, A_Y;
    public static int B_WIDTH, B_HEIGHT;
    public static int B_X, B_Y;
    
    //  LDBUTTONS
    public LDButton TOP_BUTTON;
    public LDButton MID_BUTTON;
    public LDButton BOT_BUTTON;
    
    //  LDIMAGES
    public static ArrayList<LDImage> ldImages = new ArrayList<>();
    public LDImage MAIN_MENU_BG;
    public LDImage SIDEBAR;
    
    //MENU ACTIONS'
    public ArrayList<MenuAction> menuActions = new ArrayList<>();
    
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
    MainMenuApplet mm;
    public MainMenuApplet(int w, int h, MainMenuFrame hostFrame, Account account)
    {
        setMainDimensions(w,h);
        setUpBGLabels();
        setUpButtons();
        mm = this;
         // Adds MouseListener to Applet
    }
    

    
/////////////////////////////////////  Initialization /////////////////////////////////////////////////////////////////////////////////////
    
    //Sets Initial Screen Dimensions
    private void setMainDimensions(int w, int h)
    {
        WIDTH = w * Global.SCALE_FACTOR;
        A_WIDTH = A_HEIGHT = B_HEIGHT = HEIGHT = B_X = h * Global.SCALE_FACTOR;
        B_WIDTH = WIDTH - A_WIDTH;
        A_X = A_Y = B_Y = 0;
        
        this.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - WIDTH)/2,(Toolkit.getDefaultToolkit().getScreenSize().height - HEIGHT)/2, WIDTH, HEIGHT);
        this.setLayout(null);
        this.addMouseListener(new AppletMouseListener());
        this.addKeyListener(new AppletKeyListener());
        this.setFocusable(true);
        this.requestFocusInWindow();
        System.out.println(this.getKeyListeners()[0].toString());

        this.setVisible(true);
    }
    
    //Sets Up Menu BGs
    private void setUpBGLabels()
    {
        MAIN_MENU_BG = new LDImage(A_X,A_Y,A_WIDTH,A_HEIGHT,"images/MainMenuBG.png");
        SIDEBAR  = new LDImage(B_X,B_Y,B_WIDTH,B_HEIGHT,"images/sidebar.png");
    }
   
    //Sets up buttons
    private void setUpButtons()
    {
        TOP_BUTTON = new LDButton(LDButton.TOP_X,LDButton.TOP_Y,"NEW GAME", new PracticeButton(), this);
        MID_BUTTON = new LDButton(LDButton.MID_X,LDButton.MID_Y,"LOAD GAME", new PracticeButton(), this);
        BOT_BUTTON = new LDButton(LDButton.BOT_X,LDButton.BOT_Y,"DIE TRYING", new PracticeButton(), this);
    }
    
    public void paint(Graphics g)
    {
        BufferedImage masterImage = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB);
        
        for(LDImage img: ldImages)
        {
            masterImage.createGraphics().drawImage(img, img.getX(), img.getY(), null);
        }
        
        g.drawImage(masterImage,0,0,null);
        
    }
    
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    public class PracticeButton extends MenuAction
    {
        @Override
        public void act()
        {
            System.out.println("BUTTON CLICKED!!");
        }
    }
    
    
    
    
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Adds a MenuAction on top of the location of a LDButton
    public void addMenuAction(MenuAction m)
    {
        menuActions.add(m);     
    }
        
    
    //Responds to the clicks of the user and activates the MenuAction 
    // clicked on
    public class AppletMouseListener implements MouseListener
    {

        //Finds and activates the MenuAction visible on the clicked location
        @Override
        public void mouseClicked(MouseEvent e) 
        {
            int x = e.getX()/Global.SCALE_FACTOR;
            int y = e.getY()/Global.SCALE_FACTOR;

            for (MenuAction m : menuActions)
            {
                if (m.vis)
                {
                    if (x >= m.x && x <= m.x + m.w)
                        if (y >= m.y && y <= m.y + m.h)
                            m.act();
                }
            }
            
        }

        //These do nothing
        @Override
        public void mousePressed(MouseEvent e) {
        }
        @Override
        public void mouseReleased(MouseEvent e) {
        }
        @Override
        public void mouseEntered(MouseEvent e) {
        }
        @Override
        public void mouseExited(MouseEvent e) {
        }
    }
    
    public class AppletKeyListener implements KeyListener
    {

        @Override
        public void keyTyped(KeyEvent e)
        {
            System.out.println("keyTyped");
        }

        @Override
        public void keyPressed(KeyEvent e)
        {
            System.out.println("BITCH PLEASE");
            int key = e.getKeyCode();
            if(key== KeyEvent.VK_ESCAPE)
                System.exit(0);
        }

        @Override
        public void keyReleased(KeyEvent e)
        {}

        @Override
        public String toString()
        {
            return "YO THAT KEYBOARD LISTENER IS LOADED YO!";
        }
    }
}
